<?php

namespace plugin\ycMassage\app\api\controller;

use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use app\common\manager\SettingsMgr;
use plugin\ycMassage\app\model\MassageUserChannelsCategory;
use plugin\ycMassage\utils\enum\FeedbackType;
use plugin\ycMassage\utils\enum\MchQualifications;
use plugin\ycMassage\utils\enum\TechnicianQualifications;
use support\Request;

class ConfigController extends BaseController {
    protected $noNeedLogin = ["index", "channels"];
    public function index(Request $request) {
        $appid = (int)$request->appid;
        $platforms = $request->get('platforms');
        $domain = getHpConfig('domain', $appid, 'plugins_domains');
        $regionsFile = "uploads/ycMassage/regions/Regions-{$appid}.json";
        $regions_url = '';
        if (file_exists(public_path() . $regionsFile)) {
            $regions_url = $domain . '/' . $regionsFile;
        }
        $data = [
            'regions_url' => $regions_url,
            'single_mch' => 0,
            'web_name' => '',
            'web_logo' => '',
            'web_url' => '',
            'industry_personnel_name' => '技师',
            'wechat_mp_audit' => 1,
            'vip_member_name' => '开通会员',
            'wx_state' => 0,
            'wx_applet_state' => 0,
            'alipay_state' => 0,
            'wxpay_state' => 0,
            'epay_state' => 0,
            'money_state' => 0,
            'money_orders_type' => [],
            'imgcode' => 0,
            'technician_individual_settlement' => 0,
            'share_message_title' => '',
            'share_message_image' => '',
            'share_timeline_title' => '',
            'share_timeline_image' => '',
            'feedback_type' => FeedbackType::getOptions(),
            'money_min_register' => '',
            'money_register_tips' => '',
            'default_avatar' => '',
            'amap_web_key' => '',
            'amap_js_code' => '',
            'amap_service_host' => '',
            'amap_mini_key' => '',
            'map_channels' => '',
            'technician_withdrawal_fee' => 0,
            'technician_min_withdrawal_money' => 0,
            'agent_apply_tips' => '',
            'baidu_ocr' => 0,
            'force_realname' => 0,
            'force_technician_realname' => 0
        ];
        $system = getHpConfig('', $appid, 'system');
        if (!empty($system['web_name'])) {
            $data['web_name'] = $system['web_name'];
        }
        if (!empty($system['web_logo'])) {
            $data['web_logo'] = UploadService::url($system['web_logo']);
        }
        if (isset($system['imgcode'])) {
            $data['imgcode'] = (int)$system['imgcode'];
        }
        if (isset($system['wechat_mp_audit'])) {
            $data['wechat_mp_audit'] = (int)$system['wechat_mp_audit'];
        }
        if (!empty($system['vip_member_name'])) {
            $data['vip_member_name'] = $system['vip_member_name'];
        }
        if (!empty($system['single_mch'])) {
            $data['single_mch'] = $system['single_mch'];
        }
        if (!empty($system['map_channels'])) {
            $data['map_channels'] = $system['map_channels'];
        }
        if (!empty($system['industry_personnel_name'])) {
            $data['industry_personnel_name'] = $system['industry_personnel_name'];
        }
        $share = getHpConfig('', $appid, 'wechat_mp_share');
        if (!empty($share['message_image'])) {
            $data['share_message_image'] = UploadService::url($share['message_image']);
        }
        if (!empty($share['timeline_image'])) {
            $data['share_timeline_image'] = UploadService::url($share['timeline_image']);
        }
        if (!empty($share['message_title'])) {
            $data['share_message_title'] = $share['message_title'];
        }
        if (!empty($share['timeline_title'])) {
            $data['share_timeline_title'] = $share['timeline_title'];
        }
        $pay = SettingsMgr::getChildren($appid, 'pay');
        if (isset($pay['alipay_state'])) {
            $data['alipay_state'] = $pay['alipay_state'];
        }
        if (isset($pay['wxpay_state'])) {
            $data['wxpay_state'] = $pay['wxpay_state'];
        }
        if (isset($pay['epay_state'])) {
            $data['epay_state'] = $pay['epay_state'];
        }
        if (isset($pay['money_state'])) {
            $data['money_state'] = $pay['money_state'];
        }
        if (!empty($pay['money_orders_type'])) {
            $data['money_orders_type'] = $pay['money_orders_type'];
        }
        $data['wx_state'] = getHpConfig('state', $appid, 'wx');
        $applet = SettingsMgr::group($appid, 'applet');
        if (!empty($applet['applet_appid']) && !empty($applet['applet_secret'])) {
            $data['wx_applet_state'] = 1;
        }
        $technician = getHpConfig('', $appid, 'technician');
        if (isset($technician['individual_settlement'])) {
            $data['technician_individual_settlement'] = $technician['individual_settlement'];
        }
        if (!empty($technician['qualifications'])) {
            $qualifications = [];
            foreach ($technician['qualifications'] as $key => $value) {
                $qualifications[] = TechnicianQualifications::getText($value)['text'];
            }
            $data['technician_qualifications'] = $qualifications;
        }
        if (isset($technician['force_realname'])) {
            $data['force_technician_realname'] = $technician['force_realname'];
        }
        if (isset($technician['show_qualifications'])) {
            $data['show_qualifications'] = $technician['show_qualifications'];
        }
        if (isset($technician['show_age'])) {
            $data['show_age'] = $technician['show_age'];
        }
        $mch = getHpConfig('', $appid, 'mch');
        if (!empty($mch['qualifications'])) {
            $qualifications = [];
            foreach ($mch['qualifications'] as $key => $value) {
                $qualifications[] = MchQualifications::getText($value)['text'];
            }
            $data['mch_qualifications'] = $qualifications;
        }
        if (isset($mch['allow_technician_scheduling'])) {
            $data['allow_technician_scheduling'] = $mch['allow_technician_scheduling'];
        }
        $money = getHpConfig('', $appid, 'money');
        if (!empty($money['money_register_tips'])) {
            $data['money_register_tips'] = $money['money_register_tips'];
        }
        if (isset($money['money_min_register'])) {
            $data['money_min_register'] = $money['money_min_register'];
        }
        $orders = getHpConfig('', $appid, 'orders');
        if (isset($orders['arrival_photo'])) {
            $data['arrival_photo'] = $orders['arrival_photo'];
        }
        if (!empty($orders['arrival_photo_tips'])) {
            $data['arrival_photo_tips'] = $orders['arrival_photo_tips'];
        }
        if (isset($orders['service_start_photo'])) {
            $data['service_start_photo'] = $orders['service_start_photo'];
        }
        if (!empty($orders['service_start_photo_tips'])) {
            $data['service_start_photo_tips'] = $orders['service_start_photo_tips'];
        }
        if (isset($orders['service_end_photo'])) {
            $data['service_end_photo'] = $orders['service_end_photo'];
        }
        if (!empty($orders['service_end_photo_tips'])) {
            $data['service_end_photo_tips'] = $orders['service_end_photo_tips'];
        }
        if (isset($orders['to_shop_use'])) {
            $data['to_shop_use'] = $orders['to_shop_use'];
        }
        $user = getHpConfig('', $appid, 'user');
        if (!empty($user['default_avatar'])) {
            $data['default_avatar'] = UploadService::url($user['default_avatar']);
        }
        if (isset($user['force_realname'])) {
            $data['force_realname'] = $user['force_realname'];
        }
        $map = SettingsMgr::getChildren($appid, 'map');
        if (!empty($map['amap_web_key'])) {
            $data['amap_web_key'] = $map['amap_web_key'];
        }
        if (!empty($map['amap_service_host'])) {
            $data['amap_service_host'] = $map['amap_service_host'];
        } else {
            if (!empty($map['amap_js_code'])) {
                $data['amap_js_code'] = $map['amap_js_code'];
            }
        }
        if (strpos($platforms, 'MP-WEIXIN') !== false) {
            if (!empty($map['amap_mini_key'])) {
                $data['amap_mini_key'] = $map['amap_mini_key'];
            }
        }
        $withdrawal = getHpConfig('', $appid, 'withdrawal');
        if (isset($withdrawal['technician_withdrawal_fee'])) {
            $data['technician_withdrawal_fee'] = (int)$withdrawal['technician_withdrawal_fee'];
        }
        if (isset($withdrawal['technician_min_withdrawal_money'])) {
            $data['technician_min_withdrawal_money'] = (float)$withdrawal['technician_min_withdrawal_money'];
        }
        if (isset($withdrawal['agent_withdrawal_fee'])) {
            $data['agent_withdrawal_fee'] = (int)$withdrawal['agent_withdrawal_fee'];
        }
        if (isset($withdrawal['agent_min_withdrawal_money'])) {
            $data['agentn_min_withdrawal_money'] = (float)$withdrawal['agent_min_withdrawal_money'];
        }
        if (isset($withdrawal['channels_withdrawal_fee'])) {
            $data['channels_withdrawal_fee'] = (int)$withdrawal['channels_withdrawal_fee'];
        }
        if (isset($withdrawal['channels_min_withdrawal_money'])) {
            $data['channels_min_withdrawal_money'] = (float)$withdrawal['channels_min_withdrawal_money'];
        }
        if (isset($withdrawal['dealer_withdrawal_fee'])) {
            $data['dealer_withdrawal_fee'] = (int)$withdrawal['dealer_withdrawal_fee'];
        }
        if (isset($withdrawal['dealer_min_withdrawal_money'])) {
            $data['dealer_min_withdrawal_money'] = (float)$withdrawal['dealer_min_withdrawal_money'];
        }
        if (isset($withdrawal['salesman_withdrawal_fee'])) {
            $data['salesman_withdrawal_fee'] = (int)$withdrawal['salesman_withdrawal_fee'];
        }
        if (isset($withdrawal['salesman_min_withdrawal_money'])) {
            $data['salesman_min_withdrawal_money'] = (float)$withdrawal['salesman_min_withdrawal_money'];
        }
        $new_user_gift = getHpConfig('', $appid, 'new_user_gift');
        if (isset($new_user_gift['new_user_gift_state'])) {
            $data['new_user_gift_state'] = (int)$new_user_gift['new_user_gift_state'];
        }
        $new_technicians_gift = getHpConfig('', $appid, 'new_technicians_gift');
        if (isset($new_technicians_gift['new_technicians_gift_state'])) {
            $data['new_technicians_gift_state'] = (int)$new_technicians_gift['new_technicians_gift_state'];
        }
        $agent = getHpConfig('', $appid, 'agent');
        if (isset($agent['agent_apply_tips'])) {
            $data['agent_apply_tips'] = $agent['agent_apply_tips'];
        }
        $baidu_ocr = getHpConfig('', $appid, 'baidu_ocr');
        if (isset($baidu_ocr['state'])) {
            $data['baidu_ocr'] = $baidu_ocr['state'];
        }
        return $this->successRes($data);
    }
    public function channels(Request $request) {
        $appid = (int)$request->appid;
        $data = [
            'valid_time' => 0,
            'commission' => 0,
            'platform_commission' => 0,
            'channels_apply_tips' => '',
            'category' => []
        ];
        $channels = getHpConfig('', $appid, 'channels');
        if (isset($channels['valid_time'])) {
            $data['valid_time'] = (int)$channels['valid_time'];
        }
        if (isset($channels['commission'])) {
            $data['commission'] = (int)$channels['commission'];
        }
        if (isset($channels['platform_commission'])) {
            $data['platform_commission'] = (int)$channels['platform_commission'];
        }
        if (isset($channels['channels_apply_tips'])) {
            $data['channels_apply_tips'] = $channels['channels_apply_tips'];
        }
        $data['category'] = MassageUserChannelsCategory::getOptions($appid);
        return $this->successRes($data);
    }
    public function dealer(Request $request) {
        $appid = (int)$request->appid;
        $data = [
            'state' => 'no',
            'apply_examine' => 'yes',
            'apply_price' => 0,
            'apply_refund' => 'yes',
            'apply_refund_text' => '',
            'level' => 1,
            'proportion' => 0,
            'proportion_level_1' => 0,
            'proportion_level_2' => 0,
            'proportion_level_3' => 0,
        ];
        $config = getHpConfig('', $appid, 'dealer');
        if (isset($config['state'])) {
            $data['state'] = $config['state'];
        }
        if (isset($config['apply_examine'])) {
            $data['apply_examine'] = $config['apply_examine'];
        }
        if (isset($config['apply_price'])) {
            $data['apply_price'] = (int)$config['apply_price'];
        }
        if (isset($config['apply_refund'])) {
            $data['apply_refund'] = $config['apply_refund'];
        }
        if (isset($config['apply_refund_text'])) {
            $data['apply_refund_text'] = $config['apply_refund_text'];
        }
        if (isset($config['level'])) {
            $data['level'] = (int)$config['level'];
        }
        if (isset($config['proportion'])) {
            $data['proportion'] = (int)$config['proportion'];
        }
        if (isset($config['proportion_level_1'])) {
            $data['proportion_level_1'] = (int)$config['proportion_level_1'];
        }
        if (isset($config['proportion_level_2'])) {
            $data['proportion_level_2'] = (int)$config['proportion_level_2'];
        }
        if (isset($config['proportion_level_3'])) {
            $data['proportion_level_3'] = (int)$config['proportion_level_3'];
        }
        return $this->successRes($data);
    }
    public function salesman(Request $request) {
        $appid = (int)$request->appid;
        $data = [
            'commission' => 0,
            'platform_commission' => 0,
            'apply_tips' => '',
        ];
        $salesman = getHpConfig('', $appid, 'salesman');
        if (isset($salesman['commission'])) {
            $data['commission'] = (int)$salesman['commission'];
        }
        if (isset($salesman['platform_commission'])) {
            $data['platform_commission'] = (int)$salesman['platform_commission'];
        }
        if (isset($salesman['apply_tips'])) {
            $data['apply_tips'] = $salesman['apply_tips'];
        }
        return $this->successRes($data);
    }
}
